package com.example.Goods.controller;


import com.example.Goods.domain.Bo.addGoodsBo;
import com.example.Goods.domain.Goods;
import com.example.Goods.domain.Vo.goodsListVo;
import com.example.Goods.service.IGoodsService;
import com.example.comon.domain.LoginUser;
import com.example.comon.domain.R;
import com.example.comon.utils.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;


@RestController
@RequestMapping("/goods")
@Api(value = "goods",tags = "二手商品")
public class GoodsController {
    @Autowired
    private IGoodsService goodsService;

    @GetMapping("/getGoodsList")
    @ApiOperation("获取商品列表")
    public R<List<goodsListVo>> getGoodsList(addGoodsBo bo){
        Goods goods = new Goods();
        goods.setGoodsId(bo.getGoodsId());
        goods.setGoodsName(bo.getGoodsName());
        goods.setPushUserId(bo.getPushUserId());
        goods.setState(bo.getState());
        List<goodsListVo> list= goodsService.selectGoods(goods);
        return R.success(list);
    }
    @GetMapping("/getGoodsByUserId")
    @ApiOperation("根据用户ID查找商品")
    public R getGoodsByUserId(){
        LoginUser loginUser = UserUtil.getLoginUser();
        Integer userId = loginUser.getUserId();
        List<Goods> list = goodsService.getGoodsByUserId(userId);
        return R.success(list);
    }

    @GetMapping("/getOtherGoodsList")
    @ApiOperation("获取他人商品列表")
    public R getOtherGoodsList(){
        LoginUser loginUser = UserUtil.getLoginUser();
        Integer userId = loginUser.getUserId();
        List<Goods> list = goodsService.getOtherGoodsList(userId);
        return R.success(list);
    }
    @PostMapping("/addGoods")
    @ApiOperation("增加商品")
    public R<Goods> addGoods(@RequestBody addGoodsBo bo){
        LoginUser loginUser = UserUtil.getLoginUser();
        Integer userId = loginUser.getUserId();
        Random random = new Random();
        Goods goods = new Goods();
        goods.setGoodsId(bo.getGoodsId());
        goods.setGoodsName(bo.getGoodsName());
        goods.setText(bo.getText());
        goods.setPrice(bo.getPrice());
        goods.setImg(bo.getImg());
        goods.setPushUserId(userId);
        goods.setState(bo.getState());
        goods.setBrowse(String.valueOf(random.nextInt(200)));
        goods.setChat(String.valueOf(random.nextInt(50)));
        goods.setWant(String.valueOf(random.nextInt(10)));
        goodsService.insertGoods(goods);
        return R.success(goods);
    }

    @PostMapping("/updateGoodsById")
    @ApiOperation("修改商品信息")
    public R updateGoodsById(@RequestBody Goods goods){
        goodsService.updateGoodsById(goods);
        return R.success();

    }

    @PostMapping("/delGoodsById")
    @ApiOperation("删除商品")
    public R delGoodsById(String goodsId){
        goodsService.delGoodsById(goodsId);
        return R.success();

    }
}
